<!-- eslint-disable vue/valid-attribute-name -->
<template>
  <el-card>
    <el-table
      :data="MenuArr"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
    >
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column
        label="修改时间"
        prop="updateTime"
        :formatter="formatDate"
      ></el-table-column>
      <el-table-column label="操作" width="280px" fixed="right">
        <template #="{ row }">
          <el-button
            type="primary"
            size="small"
            icon="Plus"
            :disabled="row.parent_id ? true : false"
            @click="addMenu(row)"
          >
            添加菜单
          </el-button>
          <el-button
            type="warning"
            size="small"
            icon="Edit"
            :disabled="!row.parent_id ? true : false"
            @click="updateMenu(row)"
          >
            编辑
          </el-button>
          <el-popconfirm
            :title="`你确定要删除${row.name}?`"
            width="260px"
            @confirm="removeMenu(row.id)"
          >
            <template #reference>
              <el-button
                type="danger"
                size="small"
                icon="Delete"
                :disabled="!row.parent_id ? true : false"
              >
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 对话框组件：添加或者更新已有的菜单的数据结构 -->
  <el-dialog v-model="dialogVisible">
    <!-- 表单组件：收集新增与已有的菜单的数据 -->
    <el-form :model="menuData" :rules="rules" ref="menuForm">
      <el-form-item label="名称" prop="name">
        <el-input
          placeholder="请输入菜单名称"
          v-model="menuData.name"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reqAllMenus, reqAddOrUpdateMenu, reqRemoveMenu } from '@/api/acl/menu'
import type { MenusList, MenuParams } from '@/api/acl/menu/type.js'
import { ElMessage } from 'element-plus'
import { nextTick, onMounted, reactive, ref } from 'vue'
// 存储菜单的数据
let MenuArr = ref<MenusList>([])
// 对话框组件
let dialogVisible = ref<boolean>(false)
// 携带的参数
let menuData = reactive<MenuParams>({
  name: '',
})
let menuForm = ref<any>()
onMounted(() => {
  getHasMenus()
})
// 获取菜单数据
const getHasMenus = async () => {
  let result: any = await reqAllMenus()
  if (result.code == 200) {
    MenuArr.value = result.data
  }
}
// 格式化时间
const formatDate = (row: any) => {
  return new Date(row.createTime).toLocaleString()
}
// 添加菜单按钮的回调
const addMenu = (row: any) => {
  // 清空数据
  Object.assign(menuData, {
    id: 0,
    name: '',
    parent_id: 0,
  })
  dialogVisible.value = true
  menuData.parent_id = row.id as number
  nextTick(() => {
    menuForm.value.clearValidate()
  })
}
// 打开编辑已有菜单的对话框
const updateMenu = (row: any) => {
  dialogVisible.value = true
  Object.assign(menuData, row)
}
// 确定按钮的回调
const save = async () => {
  let result: any = await reqAddOrUpdateMenu(menuData)
  if (result.code == 200) {
    dialogVisible.value = false
    ElMessage({
      type: 'success',
      message: menuData.id ? '更新成功' : '添加成功',
    })
    getHasMenus()
  } else {
    ElMessage({
      type: 'error',
      message: menuData.id ? '更新失败' : '添加失败',
    })
    dialogVisible.value = false
    getHasMenus()
  }
}
// 删除按钮的回调
const removeMenu = async (id: number) => {
  let result: any = await reqRemoveMenu(id)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    getHasMenus()
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    })
  }
}
// 校验
const validatormenuName = (value: any, callback: any) => {
  if (value.trim().length >= 4) {
    callback()
  } else {
    callback(new Error('请输入4个字符以上'))
  }
}
const rules = {
  name: [{ required: true, trigger: 'blur', validator: validatormenuName }],
}
</script>

<style lang="scss" scoped></style>
